package top.anka1.fxgui;

import javafx.scene.control.*;
import top.anka1.ims.*;
import top.anka1.ims.ZooManagementSystem.*;
import top.anka1.ims.BouncingShapes.bounceboxapp.*;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.util.ArrayList;
/**
 * ZooController class handles the GUI interactions for the zoo management system.
 * It provides methods to display animal information, move animals between zones,
 * add or remove animals, and show copyright information.
 * @author Anka
 */
public class ZooController {
    ZooGUI zooGUI = new ZooGUI();
    Stage stage;
    Stage stage2;
    BankingService bankingService = new BankingService();

    public void setStage(Stage stage) {
        this.stage = stage;
    }
    public void setStage2(Stage stage2) {
        this.stage2 = stage2;
    }

    @FXML
    public void showCopyright() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(ZooApplication.class.getResource("About_Copyright.fxml"));
            AboutCopyrightController aboutCopyrightController = fxmlLoader.getController();
            Stage cpoyRightStage = new Stage();
            cpoyRightStage.setScene(new Scene(fxmlLoader.load()));
            cpoyRightStage.setTitle("About");
            cpoyRightStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void exitZoo() {
        Alert confirmationAlert = new Alert(Alert.AlertType.CONFIRMATION);
        confirmationAlert.setTitle("Exit Confirmation");
        confirmationAlert.setHeaderText("Are you sure you want to exit?");
        confirmationAlert.setContentText("Click OK to exit, or Cancel to return to the application.");
        confirmationAlert.showAndWait().ifPresent(response -> {
            if (response == javafx.scene.control.ButtonType.OK) {
                //System.exit(0);
                try {
                    stage.hide();
                }
                catch (Exception e) {
                    //e.printStackTrace();
                }
                try {
                    stage2.hide();
                }
                catch (Exception e) {
                    //e.printStackTrace();
                }
            }
        });
    }
    @FXML
    public void displaySouthernZoneZooAnimals() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(ZooApplication.class.getResource("displayAnimals-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());

            // Get the controller instance
            DisplayAnimalsController controller = fxmlLoader.getController();

            // Get the list of animals and their capacity information
            ArrayList<Animal> animalList = zooGUI.getAnimalList("Southern-Zone Zoo");
            int count = zooGUI.getsouthernZoneZoo().getCounter();
            int capacity = zooGUI.getsouthernZoneZoo().animals.length;

            // Pass variable
            controller.setData("Southern-Zone Zoo", animalList, count, capacity);

            // Display a new window
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.setTitle("Southern-Zone Zoo");
            stage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void displayNorthernZoneZooAnimals() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(ZooApplication.class.getResource("displayAnimals-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());

            // Get the controller instance
            DisplayAnimalsController controller = fxmlLoader.getController();

            // Get the list of animals and their capacity information
            ArrayList<Animal> animalList = zooGUI.getAnimalList("Northern-Zone Zoo");
            int count = zooGUI.getsouthernZoneZoo().getCounter();
            int capacity = zooGUI.getsouthernZoneZoo().animals.length;

            // Pass variable
            controller.setData("Northern-Zone Zoo", animalList, count, capacity);

            // Display a new window
            Stage stage = new Stage();
            stage.setScene(scene);
            stage.setTitle("Northern-Zone Zoo");
            stage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void MoveFromSouthernZoneToNorthernZone() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(ZooApplication.class.getResource("moveAnimal-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());

            // Get the controller instance
            MoveAnimalController controller = fxmlLoader.getController();

            // Get the list of animals and their capacity information
            ArrayList<Animal> animalList = zooGUI.getAnimalList("Southern-Zone Zoo");

            // Display a new window
            Stage stage = new Stage();

            // Pass variable
            controller.setData(zooGUI.getsouthernZoneZoo(), zooGUI.getnorthernZoneZoo(), animalList, stage, zooGUI);

            stage.setScene(scene);
            stage.setTitle("Move Animal");
            stage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void MoveFromNorthernZoneToSouthernZone() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(ZooApplication.class.getResource("moveAnimal-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());

            // Get the controller instance
            MoveAnimalController controller = fxmlLoader.getController();

            // Get the list of animals and their capacity information
            ArrayList<Animal> animalList = zooGUI.getAnimalList("Northern-Zone Zoo");

            // Display a new window
            Stage stage = new Stage();

            // Pass variable
            controller.setData(zooGUI.getnorthernZoneZoo(), zooGUI.getsouthernZoneZoo(), animalList, stage, zooGUI);

            stage.setScene(scene);
            stage.setTitle("Move Animal");
            stage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void AddAnimalToSouthernZone() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(ZooApplication.class.getResource("addAnimal-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());

            // Get the controller instance
            AddAnimalController controller = fxmlLoader.getController();

            // Display a new window
            Stage stage = new Stage();

            // Pass variable
            controller.setData(stage, zooGUI.getsouthernZoneZoo());

            stage.setScene(scene);
            stage.setTitle("Add Animal");
            stage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void AddAnimalToNorthernZone() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(ZooApplication.class.getResource("addAnimal-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());

            // Get the controller instance
            AddAnimalController controller = fxmlLoader.getController();

            // Display a new window
            Stage stage = new Stage();

            // Pass variable
            controller.setData(stage, zooGUI.getnorthernZoneZoo());

            stage.setScene(scene);
            stage.setTitle("Add Animal");
            stage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void RemoveAnimalFromSouthernZone() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(ZooApplication.class.getResource("removeAnimal-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());

            // Get the controller instance
            RemoveAnimalController controller = fxmlLoader.getController();

            // Display a new window
            Stage stage = new Stage();

            // Pass variable
            controller.setData(stage, zooGUI.getsouthernZoneZoo());

            stage.setScene(scene);
            stage.setTitle("Remove Animal");
            stage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void RemoveAnimalFromNorthernZone() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(ZooApplication.class.getResource("removeAnimal-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());

            // Get the controller instance
            RemoveAnimalController controller = fxmlLoader.getController();

            // Display a new window
            Stage stage = new Stage();

            // Pass variable
            controller.setData(stage, zooGUI.getnorthernZoneZoo());

            stage.setScene(scene);
            stage.setTitle("Remove Animal");
            stage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void FindAnimal() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(ZooApplication.class.getResource("findAnimal-view.fxml"));
            Scene scene = new Scene(fxmlLoader.load());

            // Get the controller instance
            FindAnimalController controller = fxmlLoader.getController();

            // Display a new window
            Stage stage = new Stage();

            // Pass variable
            controller.setData(stage, zooGUI);

            stage.setScene(scene);
            stage.setTitle("Find Animal");
            stage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @FXML
    public void ZooVisualization() {
        ZooSplitBarVisualizer visualizer = new ZooSplitBarVisualizer(800, 600);
        visualizer.visualizeZooAnimals(zooGUI.getsouthernZoneZoo(), zooGUI.getnorthernZoneZoo());
    }
    @FXML
    public void getTransportCost_SouthToNorth() {
        double cost = zooGUI.getZooManager().getTransportCost("Southern-Zone Zoo", "Northern-Zone Zoo");
        Alert alert;
        if (cost != -1) {
            alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("Transport Cost");
            alert.setHeaderText("Transport Cost from Southern-Zone Zoo to Northern-Zone Zoo");
            alert.setContentText("The total transport cost is: " + String.format("¥%.2f", cost));
        } else {
            alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Error");
            alert.setHeaderText("Transport Cost Error");
            alert.setContentText("No cost record found.");
        }
        alert.showAndWait();
    }
    @FXML
    public void getTransportCost_NorthToSouth() {
        double cost = zooGUI.getZooManager().getTransportCost("Northern-Zone Zoo", "Southern-Zone Zoo");
        Alert alert;
        if (cost != -1) {
            alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("Transport Cost");
            alert.setHeaderText("Transport Cost from Northern-Zone Zoo to Southern-Zone Zoo");
            alert.setContentText("The total transport cost is: " + String.format("¥%.2f", cost));
        } else {
            alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Error");
            alert.setHeaderText("Transport Cost Error");
            alert.setContentText("No cost record found.");
        }
        alert.showAndWait();
    }
    @FXML
    public void buyTickets() {
        TextInputDialog dialog = new TextInputDialog();
        dialog.setTitle("But Zoo Ticket");
        dialog.setHeaderText("Enter the number of tickets you want to buy:");
        dialog.setContentText("Number of tickets:");
        dialog.showAndWait().ifPresent(response -> {
            try {
                int numTickets = Integer.parseInt(response);
                if (numTickets <= 0) {
                    Alert alert = new Alert(Alert.AlertType.ERROR);
                    alert.setTitle("Invalid Input");
                    alert.setHeaderText("Invalid number of tickets");
                    alert.setContentText("Please enter a positive number.");
                    alert.showAndWait();
                } else {
                    double totalCost = 10 * numTickets; // Assuming each ticket costs ¥10
                    TextInputDialog paymentDialog = new TextInputDialog();
                    paymentDialog.setTitle("Payment");
                    paymentDialog.setHeaderText("Please enter your payment method:");
                    paymentDialog.setContentText("Bank Account:");
                    paymentDialog.showAndWait().ifPresent(paymentMethod -> {
                        try {
                            // Assuming the payment method is a bank account number
                            String accountNumber = paymentMethod;
                            if (bankingService.withdrawWithSpecifiedTask(accountNumber, totalCost, "Zoo Ticket Purchase")) {
                                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                                alert.setTitle("Payment Successful");
                                alert.setHeaderText("Payment Successful");
                                alert.setContentText("You have successfully purchased " + numTickets + " tickets. Your remaining balance is: ¥" + String.format("%.2f", bankingService.getBalance(accountNumber)));
                                alert.showAndWait();
                            } else {
                                Alert alert = new Alert(Alert.AlertType.ERROR);
                                alert.setTitle("Payment Failed");
                                alert.setHeaderText("Payment Failed");
                                alert.setContentText("Insufficient funds or invalid account number.");
                                alert.showAndWait();
                            }
                        } catch (Exception e) {
                            Alert alert = new Alert(Alert.AlertType.ERROR);
                            alert.setTitle("Payment Error");
                            alert.setHeaderText("Payment Failed");
                            alert.setContentText("An error occurred while processing your payment.");
                            alert.showAndWait();
                        }
                    });
                }
            } catch (NumberFormatException e) {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("Invalid Input");
                alert.setHeaderText("Invalid number of tickets");
                alert.setContentText("Please enter a valid number.");
                alert.showAndWait();
            }
        });
    }
}
